package cn.edu.csust.cvs.management.support;

import com.tencentcloudapi.asr.v20190614.AsrClient;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskRequest;
import com.tencentcloudapi.asr.v20190614.models.CreateRecTaskResponse;
import com.tencentcloudapi.asr.v20190614.models.DescribeTaskStatusRequest;
import com.tencentcloudapi.asr.v20190614.models.DescribeTaskStatusResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@Component
@Slf4j
public class STTSupport {

    private String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return Base64.getEncoder().encodeToString(b);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public Long stt(String filePath){
        try{
            String data = encryptToBase64(filePath);
            Credential cred = new Credential("AKIDp6B3f0PCxTotiIKqnWzJNAhGiGAq2f8L", "54oKbYLr8AvRkngeNsAE4d16wYu2tV2J");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            AsrClient client = new AsrClient(cred, "", clientProfile);

            CreateRecTaskRequest req = new CreateRecTaskRequest();
            req.setEngineModelType("16k_zh");
            req.setChannelNum(1L);
            req.setResTextFormat(0L);
            req.setSourceType(1L);
            req.setData(data);
            CreateRecTaskResponse resp = client.CreateRecTask(req);
            return resp.getData().getTaskId();
        } catch (TencentCloudSDKException e) {
            log.error("speech to text failed");
        }
        return 0L;
    }

    public String queryResult(long taskId) {
        try {
            Credential cred = new Credential("AKIDp6B3f0PCxTotiIKqnWzJNAhGiGAq2f8L", "54oKbYLr8AvRkngeNsAE4d16wYu2tV2J");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("asr.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            AsrClient client = new AsrClient(cred, "", clientProfile);

            DescribeTaskStatusRequest req = new DescribeTaskStatusRequest();
            req.setTaskId(taskId);

            DescribeTaskStatusResponse resp = client.DescribeTaskStatus(req);
            return resp.getData().getResult();
        } catch (TencentCloudSDKException e) {
            log.error("query recognition result failed");
        }
        return null;
    }
}
